<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/12
 * Time: 16:49
 */

namespace App\Modules\Withdraw\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class Withdraw extends Model
{
    protected $table = 'withdraw_cashes';

    //protected $fillable = ['pid','name','desc','link','state','created_at'];
    public static function getWithdrawCashList($params, $isAll = false)
    {
        $search_object = self::orderBy('withdraw_cashes.created_at', 'desc')
            ->leftjoin('payments', 'payments.withdraw_cash_id', '=', 'withdraw_cashes.id')
            ->select(DB::raw("withdraw_cashes.*,payments.pay_time"))
            ->whereIn('withdraw_cashes.status', ['自动打款成功', '人工打款成功'])
            ->orderBy('withdraw_cashes.id', 'desc');

        if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
            $search_object->where('withdraw_cashes.distribution_channel_id', $params['distribution_channel_id']);
        }


        if (isset($params['start_date']) && $params['start_date']) $search_object->where(DB::raw("date(withdraw_cashes.created_at)"), '>=', $params['start_date']);
        if (isset($params['end_date']) && $params['end_date']) $search_object->where(DB::raw("date(withdraw_cashes.created_at)"), '<=', $params['end_date']);
        if (isset($params['status']) && $params['status']) $search_object->where('withdraw_cashes.status', '=', $params['status']);
        if (isset($params['all_channels']) && $params['all_channels']) $search_object->whereIn('withdraw_cashes.distribution_channel_id', $params['all_channels']);
        //\Log::info($search_object->toSql());
        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }

    public static function getVipWithdrawCashList($params, $isAll = false)
    {
        $search_object = self::join('distribution_channels', 'withdraw_cashes.distribution_channel_id', '=', 'distribution_channels.id')
            ->join('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
            ->join('payments', 'payments.withdraw_cash_id', '=', 'withdraw_cashes.id')
            ->select(
                'withdraw_cashes.id',
                'withdraw_cashes.account_bank as bank_account',
                'withdraw_cashes.account_name as account_name',
                'withdraw_cashes.amount',
                'payments.pay_time',
                'withdraw_cashes.status',
                'withdraw_cashes.created_at',
                'channel_users.phone',
                'distribution_channels.id as distribution_channel_id',
                'distribution_channels.nickname',
                'distribution_channels.site_nick_name'
            )
            ->whereIn('withdraw_cashes.status', ['自动打款成功', '人工打款成功'])
            ->orderBy('withdraw_cashes.id', 'desc');

        if (isset($params['start_date']) && $params['start_date']) $search_object->where(DB::raw("date(withdraw_cashes.created_at)"), '>=', $params['start_date']);
        if (isset($params['end_date']) && $params['end_date']) $search_object->where(DB::raw("date(withdraw_cashes.created_at)"), '<=', $params['end_date']);
        if (isset($params['status']) && $params['status']) $search_object->where('withdraw_cashes.status', '=', $params['status']);
        if (isset($params['phone']) && $params['phone']) $search_object->where('channel_users.phone', '=', $params['phone']);
        if (isset($params['all_channels']) && $params['all_channels']) $search_object->whereIn('withdraw_cashes.distribution_channel_id', $params['all_channels']);

        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }

    public static function getWithdrawCashAmount($params)
    {
        $search_object = self::leftjoin('payments', 'payments.withdraw_cash_id', '=', 'withdraw_cashes.id')
            ->whereIn('withdraw_cashes.status', ['自动打款成功', '人工打款成功']);

        if (isset($params['distribution_channel_id']) && $params['distribution_channel_id']) {
            $search_object->where('withdraw_cashes.distribution_channel_id', $params['distribution_channel_id']);
        }
        if (isset($params['start_date']) && $params['start_date']) $search_object->where(DB::raw("date(withdraw_cashes.created_at)"), '>=', $params['start_date']);
        if (isset($params['end_date']) && $params['end_date']) $search_object->where(DB::raw("date(withdraw_cashes.created_at)"), '<=', $params['end_date']);
        if (isset($params['date']) && $params['date']) $search_object->where(DB::raw("date(withdraw_cashes.created_at)"), '=', $params['date']);
        if (isset($params['status']) && $params['status']) $search_object->where('withdraw_cashes.status', '=', $params['status']);
        if (isset($params['all_channels']) && $params['all_channels']) $search_object->whereIn('withdraw_cashes.distribution_channel_id', $params['all_channels']);
        //\Log::info($search_object->toSql());
        return $search_object->sum('withdraw_cashes.amount');
    }
}